

# 数学
# class Solution(object):
#     def stoneGame(self, piles):
#         return True

import functools
class Solution(object):
    def stoneGame(self, piles):
        @functools.cache
        def search(i, j):
            if i == j:
                return piles[i]
            else:
                return max(piles[i] - search(i + 1, j), piles[j] - search(i, j - 1))
        return search(0, len(piles) - 1) > 0
        


data = Solution()
piles = [5, 3, 4, 5]
print(data.stoneGame(piles))
piles = [3, 7, 2, 3]
print(data.stoneGame(piles))